package com.yzt.zhuju.voice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

import com.yzt.p_base.AppUtils;

import java.io.IOException;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class MediaPlayerManager {

    private static MediaPlayerManager playerManager;

    private final MediaPlayer mediaPlayer;

    private PlayCallback callback;

    private final Context context;

    private final AudioManager audioManager;

    public static MediaPlayerManager getManager() {
        if (playerManager == null) {
            synchronized (MediaPlayerManager.class) {
                playerManager = new MediaPlayerManager();
            }
        }
        return playerManager;
    }

    private MediaPlayerManager() {
        this.context = AppUtils.getContext();
        audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        mediaPlayer = new MediaPlayer();
    }

    /**
     * 切换到外放
     */
    public void changeToSpeaker() {
        audioManager.setMode(AudioManager.MODE_NORMAL);
        audioManager.setSpeakerphoneOn(true);
    }

    /**
     * 切换到耳机模式
     */
    public void changeToHeadset() {
        audioManager.setSpeakerphoneOn(false);
    }

    /**
     * 判断当前是否插着耳机
     */
    public boolean isHeadset() {
        return audioManager.isWiredHeadsetOn() || audioManager.isBluetoothA2dpOn();
    }

    /**
     * 播放回调接口
     */
    public interface PlayCallback {

        /**
         * 音乐准备完毕
         */
        void onPrepared();

        /**
         * 音乐播放完成
         */
        void onComplete();

        /**
         * 音乐停止播放
         */
        void onStop();
    }

    /**
     * 播放音乐
     *
     * @param path 音乐文件路径
     */
    public void play(String path) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(context, Uri.parse(path));
            mediaPlayer.prepare();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    if (null != callback) {
                        callback.onPrepared();
                    }
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @SuppressLint("WrongConstant")
    public void playAssets(String assetsName, boolean loop) {
        try {
            AssetManager am = context.getAssets();
            AssetFileDescriptor fileDescriptor = am.openFd(assetsName);
            mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(AudioManager.STREAM_ALARM).build());
            mediaPlayer.reset();
            mediaPlayer.setLooping(loop);
            mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                    fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            mediaPlayer.prepare();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 停止播放
     */
    public void stop() {
        if (isPlaying()) {
            try {
                mediaPlayer.stop();
                if (null != callback) {
                    callback.onStop();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 是否正在播放
     *
     * @return 正在播放返回true, 否则返回false
     */
    public boolean isPlaying() {
        return mediaPlayer != null && mediaPlayer.isPlaying();
    }
}
